<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="orderSn">
      <el-input v-model="dataForm.orderSn" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="分类id" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="分类id"></el-input>
    </el-form-item>
    <el-form-item label="组合的销售属性值" prop="saleAttrValueArray">
      <el-input v-model="dataForm.saleAttrValueArray" placeholder="组合的销售属性值"></el-input>
    </el-form-item>
    <el-form-item label="基础价格" prop="basePrice">
      <el-input v-model="dataForm.basePrice" placeholder="基础价格"></el-input>
    </el-form-item>
    <el-form-item label="销售属性额外价格总计" prop="saleAttrExtraPrice">
      <el-input v-model="dataForm.saleAttrExtraPrice" placeholder="销售属性额外价格总计"></el-input>
    </el-form-item>
    <el-form-item label="优惠" prop="couponAmount">
      <el-input v-model="dataForm.couponAmount" placeholder="优惠"></el-input>
    </el-form-item>
    <el-form-item label="实际应付价格" prop="realAmount">
      <el-input v-model="dataForm.realAmount" placeholder="实际应付价格"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderSn: '',
          productId: '',
          categoryId: '',
          saleAttrValueArray: '',
          basePrice: '',
          saleAttrExtraPrice: '',
          couponAmount: '',
          realAmount: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          orderSn: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ],
          saleAttrValueArray: [
            { required: true, message: '组合的销售属性值不能为空', trigger: 'blur' }
          ],
          basePrice: [
            { required: true, message: '基础价格不能为空', trigger: 'blur' }
          ],
          saleAttrExtraPrice: [
            { required: true, message: '销售属性额外价格总计不能为空', trigger: 'blur' }
          ],
          couponAmount: [
            { required: true, message: '优惠不能为空', trigger: 'blur' }
          ],
          realAmount: [
            { required: true, message: '实际应付价格不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/orderitem/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderSn = data.orderItem.orderSn
                this.dataForm.productId = data.orderItem.productId
                this.dataForm.categoryId = data.orderItem.categoryId
                this.dataForm.saleAttrValueArray = data.orderItem.saleAttrValueArray
                this.dataForm.basePrice = data.orderItem.basePrice
                this.dataForm.saleAttrExtraPrice = data.orderItem.saleAttrExtraPrice
                this.dataForm.couponAmount = data.orderItem.couponAmount
                this.dataForm.realAmount = data.orderItem.realAmount
                this.dataForm.createTime = data.orderItem.createTime
                this.dataForm.updateTime = data.orderItem.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/orderitem/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderSn': this.dataForm.orderSn,
                'productId': this.dataForm.productId,
                'categoryId': this.dataForm.categoryId,
                'saleAttrValueArray': this.dataForm.saleAttrValueArray,
                'basePrice': this.dataForm.basePrice,
                'saleAttrExtraPrice': this.dataForm.saleAttrExtraPrice,
                'couponAmount': this.dataForm.couponAmount,
                'realAmount': this.dataForm.realAmount,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
